package com.aistar.service.impl;


import com.aistar.mapper.CartMapper;
import com.aistar.pojo.*;
import com.aistar.service.CartDetailsService;
import com.aistar.service.CartService;
import com.aistar.service.ProductImagesService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CartDetailsService cartDetailsService;

    @Autowired
    private ProductImagesService productImagesService;

    @Autowired
    private ProductService productService;



    /*添加一个购车*/
    @Override
    public ServerResponse save(Cart cart) {
        int rows = cartMapper.insert(cart);
        if (rows >0){
            return ServerResponse.addSuccess();
        }else {
            return ServerResponse.addFailed();
        }

    }

    @Override
    public ServerResponse modifyStatus(Cart cart) {
        return null;
    }

    @Override
    public ServerResponse modify(Cart cart) {
        return null;
    }



    @Override
    public ServerResponse getById(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    /*用户查看购物车*/
    @Override
    public ServerResponse getAllByCustomerId(Integer customerId) {

        System.out.println("用户id："+customerId);
        //用户id  查询到购物车id
        ServerResponse serverResponse= getCartIdByCustomerId(customerId);
        System.out.println("购物车id："+serverResponse.getStatus());
        Integer cartId=0;
        double cartPrice = 0;
        if (serverResponse.getStatus() ==1 ){
            cartId = (Integer) serverResponse.getData();

        }
        System.out.println("cartID:"+cartId);

        //购物车id - 购物车详情表(多个)
        ServerResponse cartDetailsServerResponse = cartDetailsService.getAllByCartId(cartId);
        List<CartDetails> cartDetailsList = null;
        System.out.println("购物车是否有商品："+cartDetailsServerResponse.getStatus());
        if (cartDetailsServerResponse.getStatus()==1){
            cartDetailsList = ( List<CartDetails>)cartDetailsServerResponse.getData();
        }

        System.out.println("cartDetailsList:"+cartDetailsList);
       List<ProductAllVo> productAllVoList = new ArrayList<>();

        for (CartDetails cartDetails1 :cartDetailsList){
            ProductAllVo productAllVo =  new ProductAllVo();
            //每一个商品对应的数量存放到vo中
            Integer productNum = cartDetails1.getCartDetailsSize();
            productAllVo.setProductNum(productNum);
            //循环 根据一个商品id 拿到一个商品
            ServerResponse serverResponse1 =  productService.getById(cartDetails1.getProductId());
            System.out.println("serverResponse1:"+serverResponse1);

            System.out.println("serverResponse1.getStatus():"+serverResponse1.getStatus());

            if (serverResponse1.getStatus()==1){
                productAllVo.setProduct((Product) serverResponse1.getData());
                //这是多个同样商品的价格
                double productPrice = productAllVo.getProduct().getProductPrice() * productNum;
                cartPrice = productPrice + cartPrice;
            }
            System.out.println("productAllVo:"+productAllVo);
            //根据一个商品id 拿到这个商品对应的（多个）图片路径
            ServerResponse serverResponse2 = productImagesService.getByProductId(cartDetails1.getProductId());
            System.out.println("serverResponse2:"+serverResponse2.getStatus());
            if (serverResponse2.getStatus()==1){
                List<ProductImages> productImagesList = (List<ProductImages>)serverResponse2.getData();
                List<String> stringList = new ArrayList<>();
                //for循环拿到图片
                for(ProductImages productImages :productImagesList){
                    stringList.add(productImages.getProductImagesImage());
                }
                //将图片集合放到了vo中
                productAllVo.setStringList(stringList);
            }
            productAllVoList.add(productAllVo);


        }
        System.out.println("*****productAllVo0****:"+productAllVoList);
        System.out.println("购物车的总价格："+cartPrice);



        return ServerResponse.getDataSuccess(productAllVoList);
    }

    //根据用户id查询购物车id
    @Override
    public ServerResponse getCartIdByCustomerId(Integer customerId) {
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andCartStatusEqualTo(1)
                  .andCustomerIdEqualTo(customerId);
        List<Cart> cartList =  cartMapper.selectByExample(cartExample);
        Integer cartId =0;

        if (cartList !=null && cartList.size()>0){
            cartId = cartList.get(0).getCartId();
            return ServerResponse.getDataSuccess(cartId);
        }else {
            return ServerResponse.getDataFailed("该用户没有购物车");
        }

    }



    //根据用户id查询当前用户购物车中的数量
    @Override
    public ServerResponse getCartProductCount(Integer customerId) {
        System.out.println("customerId"+customerId);
        //根据用户id查询购物车id
        ServerResponse serverResponse= getCartIdByCustomerId(customerId);
        System.out.println("购物车id："+serverResponse.getStatus());
        Integer cartId=0;
        if (serverResponse.getStatus() ==1 ){
            cartId = (Integer) serverResponse.getData();
        }
        //购物车id - 购物车详情表(多个)
        ServerResponse cartDetailsServerResponse = cartDetailsService.getAllByCartId(cartId);
        //获取购物车详情表中商品的个数

        System.out.println("购物车是否有商品："+cartDetailsServerResponse.getStatus());

        List<CartDetails> cartDetailsList = ( List<CartDetails>)cartDetailsServerResponse.getData();

        Integer count = cartDetailsList.size();
        System.out.println("service中购物车中商品的数量:"+count);
        return ServerResponse.getDataSuccess(count);
    }
}
